<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px;margin-right: 10px;" placeholder="请输入球员姓名" suffix-icon="el-icon-search" v-model="playerName"></el-input>

      <div>
      <el-input style="width: 200px;margin-right: 10px;" placeholder="请输入最小身价" suffix-icon="el-icon-search" v-model="playerMinValue"></el-input>

      <el-input style="width: 200px;margin-right: 10px;" placeholder="请输入最大身价" suffix-icon="el-icon-search" v-model="playerMaxValue"></el-input>
      
      <el-input style="width: 200px;margin-right: 10px;" placeholder="请输入球员身价" suffix-icon="el-icon-search" v-model="playerValue"></el-input>
      </div>

      <el-select v-model="leagueId" clearable placeholder="请选择联赛" style="width: 200px;margin-right: 10px;" @change="changeLeague()">
        <el-option v-for="item in leagueoptions" :key="item.id" :label="item.leagueName" :value="item.id">
            {{ item.leagueSeason+item.leagueName }}
        </el-option>
      </el-select>

      <el-select v-model="teamId" clearable placeholder="请选择球队" style="width: 300px;margin-right: 10px;">
        <el-option v-for="item in teamoptions" :key="item.id" :label="item.teamName" :value="item.id">
            {{ item.teamName}}
        </el-option>
      </el-select>

      <el-select v-model="playerPosition" clearable placeholder="请选择场上位置" style="width: 300px;margin-right: 10px;">
        <el-option v-for="item in positionoptions" :key="item.name" :label="item.name" :value="item.value">
            {{ item.name}}
        </el-option>
      </el-select> 

      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
      <el-popconfirm
          class="ml-5"
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="您确定批量删除这些数据吗？"
          @confirm="delBatch"
      >
        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>
      <el-upload action="http://localhost:9090/player/import" :show-file-list="false" accept="xlsx" :on-success="handleExcelImportSuccess" style="display: inline-block">
            <el-button type="primary" class="ml-5">导入球员 <i class="el-icon-bottom"></i></el-button>
      </el-upload>

     <el-button type="primary" @click="exp" class="ml-5">导出球员 <i class="el-icon-top"></i></el-button>

     <el-upload action="http://localhost:9090/player/ability/import" :show-file-list="false" accept="xlsx" :on-success="handleExcelImportSuccessability" style="display: inline-block">
            <el-button type="primary" class="ml-5">导入球员能力 <i class="el-icon-bottom"></i></el-button>
      </el-upload>

     <el-button type="primary" @click="expability" class="ml-5">导出球员能力 <i class="el-icon-top"></i></el-button>
    
    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="playerName" label="球员姓名"></el-table-column>
      <el-table-column prop="playerAge" label="球员年龄"></el-table-column>
      <el-table-column prop="playerImg" label="球员图片">
        <template slot-scope="scope">
          <div v-if="scope.row.playerImg">
             <img :src="scope.row.playerImg" width="100" height="100">
               </div>
          <div v-else>无图片</div>           
        </template>
      </el-table-column>
      <el-table-column prop="playerHeight" label="身高(cm)"></el-table-column>
      <el-table-column prop="playerWeight" label="体重(kg)"></el-table-column>
      <el-table-column prop="playerPosition" label="场上位置"></el-table-column>
      <el-table-column prop="playerNumber" label="球员号码"></el-table-column>
      <el-table-column prop="playerNation" label="球员国籍"></el-table-column>
      <el-table-column prop="playerValue" label="球员身价(万欧元)"></el-table-column>
      <el-table-column prop="team.teamName" label="所属球队"></el-table-column>
      <el-table-column label="操作"  width="280" align="center">
        <template slot-scope="scope">
          <el-button type="primary" @click="playerAbility(scope.row)" ><i class="el-icon-s-marketing"></i>球员能力</el-button>
          <el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="current"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog title="球员信息" :visible.sync="dialogFormVisible" width="30%" >
      <el-form label-width="80px" size="small">
        <el-form-item label="球员姓名">
          <el-input v-model="form.playerName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="球员年龄">
          <el-input v-model="form.playerAge" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="球员图片">
          <el-upload 
            class="avatar-uploader"
            :action="'http://' + serverIp +':9090/file/upload'"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
        >
          <img v-if="form.playerImg" :src="form.playerImg" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        </el-form-item>
        <el-form-item label="所属球队">
        <el-select v-model="form.teamId" clearable placeholder="请选择球队" style="width: 200px">
        <el-option v-for="item in teamoptions" :key="item.id" :label="item.teamName" :value="item.id">
            {{ item.teamName }}
        </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="球员身高">
          <el-input v-model="form.playerHeight" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="球员体重">
          <el-input v-model="form.playerWeight" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="场上位置">
          <el-select v-model="form.playerPosition" clearable placeholder="请选择场上位置" style="width: 300px">
        <el-option v-for="item in positionoptions" :key="item.name" :label="item.name" :value="item.value">
            {{ item.name}}
        </el-option>
      </el-select> 
        </el-form-item>
        <el-form-item label="球衣号码">
          <el-input v-model="form.playerNumber" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="球员国籍">
          <el-input v-model="form.playerNation" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="球员身价">
          <el-input v-model="form.playerValue" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>


    <el-dialog title="球员能力" :visible.sync="AbilityDialogVis" width="30%">
      <el-form label-width="80px" size="small" >
        <div class="gamedatainfo" style="display: flex;justify-content: center;align-items:center">
        <div>
          <h1>整体评价</h1>
        <el-form-item label="综合能力">
          <el-input v-model="Abilityform.capability" autocomplete="off" style="width:60px"></el-input>
        </el-form-item>

        <el-form-item label="进攻能力">
          <el-input v-model="Abilityform.attack" autocomplete="off" style="width:60px"></el-input>
        </el-form-item>

        <el-form-item label="防守能力">
          <el-input v-model="Abilityform.defense" autocomplete="off" style="width:60px"></el-input>
        </el-form-item>
        </div>
        <div>
          <h1>六维能力</h1>
        <el-form-item label="速度">
          <el-input v-model="Abilityform.speed" autocomplete="off" style="width:60px"></el-input>
        </el-form-item>

        <el-form-item label="力量">
          <el-input v-model="Abilityform.power" autocomplete="off" style="width:60px"></el-input>
        </el-form-item>

        <el-form-item label="射门">
          <el-input v-model="Abilityform.shoot" autocomplete="off" style="width:60px"></el-input>
        </el-form-item>

        <el-form-item label="传球">
          <el-input v-model="Abilityform.pass" autocomplete="off" style="width:60px"></el-input>
        </el-form-item>

        <el-form-item label="盘带">
          <el-input v-model="Abilityform.dribble" autocomplete="off" style="width:60px"></el-input>
        </el-form-item>

        <el-form-item label="技巧">
          <el-input v-model="Abilityform.technique" autocomplete="off" style="width:60px"></el-input>
        </el-form-item>
        </div>
        <div>
          <h1>其他能力</h1>
        <el-form-item label="任意球">
          <el-input v-model="Abilityform.freekick" autocomplete="off" style="width:60px"></el-input>
        </el-form-item>

        <el-form-item label="体能">
          <el-input v-model="Abilityform.stamina" autocomplete="off" style="width:60px"></el-input>
        </el-form-item>

        <el-form-item label="头球">
          <el-input v-model="Abilityform.header" autocomplete="off" style="width:60px"></el-input>
        </el-form-item>

        <el-form-item label="弹跳">
          <el-input v-model="Abilityform.jump" autocomplete="off" style="width:60px"></el-input>
        </el-form-item>

        <el-form-item label="守门">
          <el-input v-model="Abilityform.keeper" autocomplete="off" style="width:60px"></el-input>
        </el-form-item>

        <el-form-item label="团队合作">
          <el-input v-model="Abilityform.teamwork" autocomplete="off" style="width:60px"></el-input>
        </el-form-item>
        
        <el-form-item label="惯用脚">
          <el-select v-model="Abilityform.habitual" clearable placeholder="请选择" style="width: 100px">
                <el-option value="左脚">左脚</el-option>
                <el-option value="右脚">右脚</el-option>
          </el-select>
        </el-form-item>
        </div>
      </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveAbility">确 定</el-button>
        <el-button @click="AbilityDialogVis = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {serverIp} from "../../../public/config";

export default {
  name:'Player',
  data(){
    return{
      tableData:[],
      total:0,//总数
      current:1,//当前页
      size:5,//每页数量
      form:{},//添加或修改提交表单
      dialogFormVisible:false,
      serverIp:serverIp,
      playerName:"",//球员姓名搜索
      teamId:"",//球员所属球队id搜索
      teamoptions:[],//球队列表
      playerPosition:"",//球员场上位置搜索
      positionoptions:[],//位置列表
      playerValue:"",//球员身价搜索
      playerMaxValue:"",//球员最高身价搜索
      playerMinValue:"",//球员最低身价搜索
      leagueoptions:[],//联赛列表
      leagueId:"",//联赛搜索
      Abilityform:{
        capability:"",
        attack:"",
        defense:"",
        speed:"",
        power:"",
        shoot:"",
        pass:"",
        dribble:"",
        technique:"",
        freekick:"",
        stamina:"",
        header:"",
        jump:"",
        keeper:"",
        teamwork:"",
        habitual:"",
      },
      AbilityDialogVis:false,
      AbilityPlayerId:"",//球员能力的球员id
    }
  },
  created(){
    this.load()
  },
  methods:{
    load(){
      this.request.get("/player/pagePlayer",{
        params:{
          current:this.current,
          limit:this.size,
          playerName:this.playerName,
          teamId:this.teamId,
          playerPosition:this.playerPosition,
          playerMaxValue:this.playerMaxValue,
          playerMinValue:this.playerMinValue,
          playerValue:this.playerValue
        }
      }).then(res =>{
        this.tableData = res.pagePlayer.records
        this.total = res.total
      }),
      //查询位置数据
      this.request.get("/player/position").then(res =>{
        console.log(res)
        this.positionoptions = res.data
      }),
      //查询所有球队信息到选择框中
      this.request.get("/team/findall").then(res =>{
        console.log(res.data)
        this.teamoptions = res.data
      }),
      //查询所有联赛信息到选择框中
      this.request.get("/league/findall").then(res=>{
        console.log(res.data)
        this.leagueoptions = res.data
      })
    },
    //选择联赛时，查询该联赛的轮次和球队
    changeLeague(){
      this.request.get("/league/findTeamByLeagueId/"+this.leagueId).then(res=>{
        if(res.code === '200'){
          this.teamoptions = []
          this.teamoptions = res.data
        }else{
          this.$message.error("查找失败")
        }
      })
    },  
     //编辑球员能力值
     saveAbility(){
      this.request.post("/player/saveAbility", this.Abilityform,
      {
        params:{
        playerId:this.AbilityPlayerId
        }
      }
      ).then(res => {
          if (res.code === '200') {
            this.$message.success("保存成功")
            this.AbilityDialogVis = false
          } else {
            this.$message.error("保存失败")
          }
        })
    },

     //添加或修改球队信息
     save() {
        this.request.post("/player/saveOrUpdate", this.form).then(res => {
          if (res.code === '200') {
            this.$message.success("保存成功")
            this.dialogFormVisible = false
            this.load()
          } else {
            this.$message.error("保存失败")
          }
        })
      },
      
    //保存图片
    handleAvatarSuccess(res) {
      this.form.playerImg = res
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogFormVisible = true
    },

    playerAbility(row) {
      this.Abilityform={};
      //先获取选中的球员id值
      this.AbilityPlayerId =row.id
      console.log(this.AbilityPlayerId)
      this.AbilityDialogVis=true
      //查看球员能力值
      this.request.get("/player/getPlayerAbility",{
        //后端接收参数
        params:{
          playerId:this.AbilityPlayerId
        }
      }).then(res=>{
        if(res.data){
          this.Abilityform = res.data;
        }
      })
    },

    del(id) {
      this.request.delete("/player/delete/" + id).then(res => {
        if (res.code === '200') {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
      this.request.post("/player/delete/batch", ids).then(res => {
        if (res.code === '200') {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    reset() {
      this.playerName = ""
      this.teamId = ""
      this.playerMaxValue=""
      this.playerMinValue=""
      this.playerValue=""
      this.playerPosition=""
      this.leagueId=""
      this.load()
    },
    //分页每页大小
    handleSizeChange(size) {
      console.log(size)
      this.size = size
      this.load()
    },

    //分页第几页
    handleCurrentChange(current) {
      console.log(current)
      this.current = current
      this.load()
    },
    //导出
    exp(){
      window.open("http://localhost:9090/player/export")
    },
    //导出球员能力
    expability(){
      window.open("http://localhost:9090/player/ability/export")
    },
    //提示文件导入成功
    handleExcelImportSuccess(){
      this.$message.success("导入球员信息成功")
      this.load()
    },

    //提示球员能力信息导入成功
    handleExcelImportSuccessability(){
      this.$message.success("导入球员能力成功")
      this.load()
    }
  }
}
</script>

<style>
.headerBg {
  background: #eee!important;
}
.avatar-uploader {
  text-align: center;
  padding-bottom: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 138px;
  height: 138px;
  line-height: 138px;
  text-align: center;
}
.avatar {
  width: 138px;
  height: 138px;
  display: block;
}
</style>